import request from "../utils/request";
import Qs from 'qs';

export function getBlogList(currentPage, pageSize) {
    return new Promise((resolve, reject) => {
        request.post("/blog/list", Qs.stringify({
            currentPage,
            pageSize
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function getBlogAllList(currentPage, pageSize) {
    return new Promise((resolve, reject) => {
        request.post("/blog/listAll", Qs.stringify({
            currentPage,
            pageSize
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function getBlogById(id) {
    return new Promise((resolve, reject) => {
        request.get("/blog/blogDetails/" + id)
            .then(response => {
                resolve(response.data);
            }).catch(err => {
            reject(err);
        })
    })
}

export function updateBlogById(id, title, cover, content) {
    return new Promise((resolve, reject) => {
        request.post("/blog/update", Qs.stringify({
            id,
            title,
            cover,
            content
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function listDownloadFile(type) {
    return new Promise((resolve, reject) => {
        request.post("/file/list", Qs.stringify({
            type,
        })).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function downloadFile(fileName,id) {
    return new Promise((resolve, reject) => {
        request.post("file/download", Qs.stringify({
            fileName,id
        }),{responseType: 'arraybuffer'}).then(response => {
            resolve(response.data);
        }).catch(err => {
            reject(err);
        })
    })
}